package suanfa.datastructure;

import java.util.Arrays;

public class Array {
    private int size;
    public int[] myarray;
    public Array(int n){
        myarray=new int[n];
        size=0;
    }

    public void insert(int e)throws IndexOutOfBoundsException{
        if(size>myarray.length){
        throw new IndexOutOfBoundsException("数组满了");
        }
        myarray[size++]=e;
    }

    public void insertmid(int e,int index)throws IndexOutOfBoundsException{
        if(size>myarray.length){
            throw new IndexOutOfBoundsException("数组满了");
        }
        if(index>=0&&index>size-1) {
            //腾位置
            for(int i=size-1;i>index;i--){
                myarray[i+1]=myarray[i];
            }
            myarray[index] = e;
            size++;
        }

    }

    public static void main(String[] args) {
        Array a=new Array(3);
        a.insert(1);
        a.insert(2);
        a.insertmid(1,1);
        System.out.println(Arrays.toString(a.myarray));
    }

}
